﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Prism.Games.GalEngine.Scripts.Data.SceneElements;

namespace Prism.Games.GalEngine.Scripts.Parser
{
    public class TrademarkBgmParser : BgmParser
    {
        public TrademarkBgmParser()
        {
            this.preBgm = new BackgroundMusic() { Id = -1 };

            this.BgmManager = new Audio.AudioManager(Helper.AudioBase.AudioFactory, Helper.AudioBase.MusicPlaybackLatency);
            this.BgmManager.EnableSingleChannel = true;
        }

        public override void PresentObject(FunctionModel.SceneObject element)
        {
            if (element is BackgroundMusic)
            {
                var target = element as BackgroundMusic;

                if (target.IsCached)
                {
                    if (target.Id != preBgm.Id)
                    {
                        this.BgmManager.Reset();

                        this.BgmManager.Load(target.CacheStream, target.Id);

                        this.BgmManager.BeginPlay(target.Id);

                        preBgm = target;
                    }
                }
                //else
                //{
                //    if (target.PartUri != null)
                //    {
                //        if (target.Id != preBgm.Id)
                //        {

                //            this.BgmManager.Reset();

                //            using (var ms = Helper.Packer.GetPartSteam(target.PartUri))
                //            {
                //                var buffer = new byte[ms.Length];

                //                ms.Read(buffer, 0, (int)ms.Length);

                //                this.BgmManager.Load(buffer, target.Id);
                //            }

                //            this.BgmManager.BeginPlay(target.Id);

                //            preBgm = target;
                //        }
                //    }
                //    else
                //    {
                //        this.BgmManager.Reset();
                //    }
                //}
            }
        }
    }
}
